//
//  BMHDBManager+SearchRecord.m
//  BMHVideoMain
//
//  Created by 陈宪栋 on 2017/4/10.
//  Copyright © 2017年 Baomihua. All rights reserved.
//

#import "BMHDBManager+SearchRecord.h"
#import "BMHSearchRecordModel.h"
#import <UtilsMacro.h>
#import <FMDB.h>

NSString *const kSearchHistoryTableName = @"kSearchRecordTableName";
NSString *const kSearchHistoryRecordName = @"kSearchRecordName";

@implementation BMHDBManager (SearchRecord)
- (BOOL)createSearchRecordTable {
    FMDatabase *db = [self getDatabase];
    NSString *createSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT 0, %@ TEXT DEFAULT NULL)", kSearchHistoryTableName, kSearchHistoryRecordName];
    return [db executeUpdate:createSql];
}

- (BOOL)addSearchHistoryModel:(BMHSearchRecordModel *)model {
    BOOL result = NO;
    if ([self checkExistWithHistoryModel:model]) {
        [self deleteWithHistoryModel:model];
    }
    FMDatabase *db = [self getDatabase];
    if (db) {
        @try {
            NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (?)", kSearchHistoryTableName, kSearchHistoryRecordName];
            result = [db executeUpdate:insertSql, model.recordName];
        } @catch (NSException *exception) {
            BMHLog(@"addSearchHistoryModel : %@", exception);
        } @finally {
            [db close];
        }
    }
    return result;
}

- (BOOL)deleteAllSearchHistory {
    BOOL result = NO;
    FMDatabase *db = [self getDatabase];
    if (db) {
        @try {
            NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM %@", kSearchHistoryTableName];
            result = [db executeUpdate:deleteSql];
        } @catch (NSException *exception) {
            BMHLog(@"deleteAllSearchHistory : %@", exception);
        } @finally {
            [db close];
        }
    }
    return result;
}

- (NSArray *)querySearchHistoryWithLimit:(NSUInteger)limitCount {
    NSMutableArray *resultArray;
    FMDatabase *db = [self getDatabase];
    if (db) {
        @try {
            NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM %@ ORDER BY id DESC LIMIT %zd", kSearchHistoryTableName, limitCount];
            FMResultSet *result = [db executeQuery:querySql];
            while ([result next]) {
                if (resultArray == nil) {
                    resultArray = [NSMutableArray array];
                }
                BMHSearchRecordModel *model = [[BMHSearchRecordModel alloc] init];
                model.recordName = [result stringForColumn:kSearchHistoryRecordName];
                [resultArray addObject:model];
            }
        } @catch (NSException *exception) {
            BMHLog(@"querySearchHistory : %@", exception);
        } @finally {
            [db close];
        }
    }
    return resultArray.copy;
}

- (BOOL)deleteWithHistoryModel:(BMHSearchRecordModel *)model {
    BOOL result = NO;
    FMDatabase *db = [self getDatabase];
    if (db) {
        @try {
            NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?",kSearchHistoryTableName,kSearchHistoryRecordName];
            result = [db executeUpdate:deleteSql,model.recordName];
        }
        @catch (NSException *exception) {
            BMHLog(@"deleteWithHistoryModel : %@", exception);
        }
        @finally {
            [db close];
        }
    }
    return result;
}

- (BOOL)checkExistWithHistoryModel:(BMHSearchRecordModel *)model {
    BOOL result = NO;
    FMDatabase *db = [self getDatabase];
    if (db) {
        @try {
            NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?",kSearchHistoryTableName,kSearchHistoryRecordName];
            FMResultSet *fmResult = [db executeQuery:querySql,model.recordName];
            if ([fmResult next]) {
                result = YES;
            }
        }
        @catch (NSException *exception) {
            BMHLog(@"checkExistWithHistoryModel : %@", exception);
        }
        @finally {
            [db close];
        }
    }
    return result;
}
@end
